//
//  LoginExperssController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "LoginExperssController.h"
#import "BaseTabbarController.h"
#import "ForgetPassWordVController.h"
#import "RegistViewController.h"
#import "WuLiuDuanViewController.h"

@interface LoginExperssController () {
    NSString *typeStr;
}
@property (nonatomic, strong)UIButton *rightBtn2;//右侧按钮

@end

@implementation LoginExperssController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNaBar];
    typeStr = @"超市端";
    // 圆角
    _typeBtn.layer.masksToBounds = YES;
    _typeBtn.layer.cornerRadius = 25/2;
    _typeBtn.layer.borderWidth = 1.0;
    _typeBtn.layer.borderColor =KBtnColor.CGColor;
}
- (IBAction)selectTypeBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        typeStr = @"配送端";
    }else {
        typeStr = @"超市端";
        
    }
    
}


- (void)configureNaBar {
    //设置导航栏按钮的边距
//    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightBtn.frame= CGRectMake(0, 0, 60, 30);
//    [rightBtn setImage:[UIImage imageNamed:@"denglu_cha"] forState:(UIControlStateNormal)];
//    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
//    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
//    /**
//     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
//     */
//    negativeSpacerleft.width = -20;
//    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
//    [self.navigationController.navigationItem setHidesBackButton:YES];
    [self.navigationItem setHidesBackButton:YES];
//    [self.navigationController.navigationBar.backItem setHidesBackButton:YES];
    
    
    //设置导航栏按钮的边距
    _rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn2.frame= CGRectMake(0, 0, 100, 30);
    [_rightBtn2 setTitle:@"用户注册" forState:(UIControlStateNormal)];
    [_rightBtn2 addTarget:self action:@selector(registExperss) forControlEvents:UIControlEventTouchUpInside];
    _rightBtn2.titleLabel.font = kFont16;
    [_rightBtn2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn2];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
    
}
- (void)handleBackAxtion {
    [self.navigationController popToRootViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
//返回
- (IBAction)backAction:(UIButton *)sender {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


//注册
- (void)registExperss {
    RegistViewController *reg = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:reg animated:YES];
}
//登录
- (IBAction)loginAction:(UIButton *)sender {
    
    
    if (!_phoneTF.text.length) {
        
        [MBProgressHUD showText:@"手机号不能为空!"];
        return;
    }
    if (!_passTF.text.length) {
        [MBProgressHUD showText:@"密码不能为空!"];
        return;
    }
    
    
    if ([typeStr isEqualToString:@"超市端"]) {
        NSDictionary *parameDic = @{@"mobile":_phoneTF.text,@"password":_passTF.text,@"actoken":[Md5Apiway getMD5data:@""]};
        
        BOOL isMoble = [BasePublicMethord isMobileNumber:_phoneTF.text];
        
        if (isMoble) {
            [HttpRequest postWithURLString:POST(Shop_LoginUrl) parameters:parameDic success:^(id responseObject) {
                
                NSLog(@"%@",responseObject);
                
                
                NSDictionary *dataDic = [responseObject objectForKey:@"data"];
                NSMutableDictionary *tempDatadDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
                [tempDatadDic setObject:_phoneTF.text forKey:@"phonenumber"];
                
                NSString *codeStr = [dataDic objectForKey:@"code"];
                
                if (codeStr.integerValue == 0) {
                    //存储信息
                    [SaveDataMethord SaveLoginDataToLocationWithDic:tempDatadDic.mutableCopy];
                    [MBProgressHUD showText:@"正在为您登录...."];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [MBProgressHUD hideHUD];
                        
                        //超市端
                        UIWindow *m_widow = [[[UIApplication sharedApplication]delegate]window];
                        BaseTabbarController *rootController = [[BaseTabbarController alloc]init];
                        m_widow.rootViewController = rootController;
                        [m_widow makeKeyAndVisible];
                        
                    });
                    
                    
                    
                }else{
                    [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[dataDic objectForKey:@"msg"]]];
                }
                
                
            } failure:^(NSError *error) {
                
            }];
        }else{
            [MBProgressHUD showText:@"请填写正确的手机号!"];
        }
    }else{
        
        
        //物流端
        
        UIWindow *m_widow = [[[UIApplication sharedApplication]delegate]window];
        WuLiuDuanViewController *rootController = [[WuLiuDuanViewController alloc] init];
       m_widow.rootViewController = rootController;
      [m_widow makeKeyAndVisible];
        
        
       // [MBProgressHUD showText:@"配送端 开发中....."];
    }
    
}

//忘记密码
- (IBAction)forgetPassAction:(UIButton *)sender {
    
    ForgetPassWordVController *fg = [[ForgetPassWordVController alloc] init];
    [self.navigationController pushViewController:fg animated:YES];
}


@end
